package org.bf.sms.web.controller;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.bf.sms.domain.common.JSONResponse;
import org.bf.sms.domain.user.Account;
import org.bf.sms.exception.SMSException;
import org.bf.sms.service.login.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Sign in
 * 
 * @author fantastic
 * @version 1.0 2010-12-9
 * @since 1.0
 */
@Controller
@RequestMapping(value = "/login")
public class AccountController
{
	/** log4j */
	private static final Logger logger = Logger
			.getLogger(AccountController.class);

	private Map<String, Account> accounts = new ConcurrentHashMap<String, Account>();

	/** service */
	@Autowired
	private LoginService loginService;

	/**
	 * @param loginService
	 *            the loginService to set
	 */
	public void setLoginService(LoginService loginService)
	{
		this.loginService = loginService;
	}

	@RequestMapping(method = RequestMethod.GET)
	public String getCreateForm(Model model)
	{
		model.addAttribute(new Account());
		return "login";
	}

	/**
	 * @param httpSession
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public String eventLogin(final HttpSession httpSession)
	{
		if (this.accounts.containsKey(httpSession.getId()))
		{
			return "account/fantastic";
		}

		return "login";
	}

	/*
	 * @RequestMapping(method = RequestMethod.POST) public String create(Account
	 * account, BindingResult result) { if (result.hasErrors()) { return
	 * "account/fantastic"; } this.accounts.put(account.getId(), account);
	 * return "redirect:/sms/account/" + 1L; }
	 */
	/*
	 * @RequestMapping(value = "/{id}", method = RequestMethod.GET) public
	 * String getView(@PathVariable Long id, Model model) { Account account =
	 * this.accounts.get(id); // if (account == null) { // throw new
	 * ResourceNotFoundException(id); // } model.addAttribute(account); return
	 * "account/fantastic"; }
	 */

	/**
	 * AJAX(jquery) "get" testing
	 * 
	 * @param account
	 *            username & password
	 * @return validation result
	 */
	@RequestMapping(value = "/validate", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> eventValidate(
			@RequestParam Map<String, Object> parameter)
	{
		// if not. validate and set it into the map.
		Account account = new Account();
		account.setUsername(parameter.get("username").toString());
		account.setPassword(parameter.get("password").toString());

		// if the user has already exists, return the message.
		JSONResponse response = new JSONResponse();
		if (this.accounts.containsValue(account))
		{
			response.setCode("03");
			response.setMessage("this user has already sign in.");
			return response.toMap();
		}

		try
		{
			// validate account information
			boolean result = this.loginService.validate(account);

			// result
			if (result)
			{
				// pass result
				response.setCode("00");
			}
			else
			{
				// user not exists
				response.setCode("01");
				response.setMessage("user dose not exists.");

			}
		}
		catch (SMSException e)
		{
			// exception result
			response.setCode("02");
			response.setMessage(e.getMessage());
		}

		// return to page
		return response.toMap();
	}
}
